/*
 * @Author: taoyuqing
 * @Date: 2023-02-28 00:47:02
 * @Description: 
 */
import 'dart:math';

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class DrawLine extends CustomPainter {
  late Offset start = Offset(0, 0);
  late Offset controlPoint = Offset(0, 0);
  late Offset end = Offset(0, 0);
  late double t = 0.0;
  // ignore: prefer_typing_uninitialized_variables
  DrawLine(
      {required this.start,
      required this.controlPoint,
      required this.end,
      required this.t});
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    List<Color> colors = [
      Color.fromRGBO(124, 24, 66, 0),
      Color.fromRGBO(255, 55, 139, 1),
    ];
    List<double> colorSteps = [0, 1];
    paint.shader = ui.Gradient.linear(start, end * 0.2, colors, colorSteps);
    paint.shader = ui.Gradient.linear(end * 0.2, end * 1, colors, colorSteps);
    paint
      ..color = Color.fromARGB(167, 241, 238, 238)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 8.0;
    Path path = Path()..moveTo(start.dx, start.dy);
    for (double k = 0; k <= t; k += 0.01) {
      double dx = computeQuadraticBezier(start.dx, controlPoint.dx, end.dx, k);
      double dy = computeQuadraticBezier(start.dy, controlPoint.dy, end.dy, k);
      path.lineTo(dx, dy);
    }
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(DrawLine oldDelegate) {
    return true;
  }

  @override
  bool shouldRebuildSemantics(DrawLine oldDelegate) => false;

  double computeQuadraticBezier(double p0, double p1, double p2, double t) {
    double k = 1 - t;
    return k * k * p0 + 2 * (1 - t) * t * p1 + t * t * p2;
  }
}
